package com.example.examsystem.controller;

import com.example.examsystem.dto.CategoryDto;
import com.example.examsystem.dto.PageResponse;
import com.example.examsystem.service.CategoryService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import java.util.List;

/**
 * 学科分类控制器
 * 处理分类的CRUD操作
 */
@RestController
@RequestMapping("/categories")
@RequiredArgsConstructor
@Slf4j
public class CategoryController {
    
    private final CategoryService categoryService;
    
    /**
     * 获取分类列表
     * @param page 页码
     * @param size 每页大小
     * @param name 分类名称
     * @param code 分类代码
     * @param subjectId 学科ID
     * @param status 状态
     * @return 分页分类列表
     */
    @GetMapping
    @PreAuthorize("hasAuthority('question:category:view')")
    public ResponseEntity<PageResponse<CategoryDto>> getCategories(
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size,
            @RequestParam(required = false) String name,
            @RequestParam(required = false) String code,
            @RequestParam(required = false) Long subjectId,
            @RequestParam(required = false) String status) {
        log.info("查询分类列表 - 页码: {}, 大小: {}, 名称: {}, 代码: {}, 学科ID: {}, 状态: {}", 
                page, size, name, code, subjectId, status);
        PageResponse<CategoryDto> categories = categoryService.getCategories(page, size, name, code, subjectId, status);
        return ResponseEntity.ok(categories);
    }
    
    /**
     * 获取分类详情
     * @param id 分类ID
     * @return 分类详情
     */
    @GetMapping("/{id}")
    @PreAuthorize("hasAuthority('question:category:view')")
    public ResponseEntity<CategoryDto> getCategory(@PathVariable Long id) {
        log.info("获取分类详情 - ID: {}", id);
        CategoryDto category = categoryService.getCategory(id);
        return ResponseEntity.ok(category);
    }
    
    /**
     * 创建分类
     * @param categoryDto 分类数据
     * @return 创建的分类
     */
    @PostMapping
    @PreAuthorize("hasAuthority('question:category:create')")
    public ResponseEntity<CategoryDto> createCategory(@Valid @RequestBody CategoryDto categoryDto) {
        log.info("创建分类 - 名称: {}, 代码: {}", categoryDto.getName(), categoryDto.getCode());
        CategoryDto createdCategory = categoryService.createCategory(categoryDto);
        return ResponseEntity.ok(createdCategory);
    }
    
    /**
     * 更新分类
     * @param id 分类ID
     * @param categoryDto 分类数据
     * @return 更新的分类
     */
    @PutMapping("/{id}")
    @PreAuthorize("hasAuthority('question:category:update')")
    public ResponseEntity<CategoryDto> updateCategory(@PathVariable Long id, 
                                                     @Valid @RequestBody CategoryDto categoryDto) {
        log.info("更新分类 - ID: {}, 名称: {}", id, categoryDto.getName());
        CategoryDto updatedCategory = categoryService.updateCategory(id, categoryDto);
        return ResponseEntity.ok(updatedCategory);
    }
    
    /**
     * 删除分类
     * @param id 分类ID
     * @return 删除结果
     */
    @DeleteMapping("/{id}")
    @PreAuthorize("hasAuthority('question:category:delete')")
    public ResponseEntity<Void> deleteCategory(@PathVariable Long id) {
        log.info("删除分类 - ID: {}", id);
        categoryService.deleteCategory(id);
        return ResponseEntity.ok().build();
    }
    
    /**
     * 根据学科ID获取分类列表
     * @param subjectId 学科ID
     * @return 分类列表
     */
    @GetMapping("/by-subject/{subjectId}")
    public ResponseEntity<List<CategoryDto>> getCategoriesBySubject(@PathVariable Long subjectId) {
        log.info("根据学科ID获取分类列表 - 学科ID: {}", subjectId);
        List<CategoryDto> categories = categoryService.getCategoriesBySubject(subjectId);
        return ResponseEntity.ok(categories);
    }
}